import { _decorator, Component, Label, Node, v3 } from 'cc';
import { MapZone } from '../entity/MapZone';
import { Village } from '../entity/Village';
import { UIManager } from '../common/UIManager';
import VillageApi from '../api/VillageApi';
import { WorldController } from '../common/WorldController';
import { PrefabEnum } from '../common/PrefabEnum';
import MapApi from '../api/MapApi';
import { UserInfo } from '../common/UserInfo';
import { Soldier } from '../entity/Soldier';
const { ccclass, property } = _decorator;

@ccclass('PanelSpace')
export class PanelSpace extends Component {
    @property(Label)
    woodNUm:Label
    @property(Label)
    mudNum:Label
    @property(Label)
    ironNum:Label
    @property(Label)
    foodNum:Label
    @property(Node)
    newVillage:Node
    @property(Node)
    moveVillage:Node

    private zone:MapZone
    private villageData: Village

    start() {
        this.newVillage.on(Node.EventType.TOUCH_END, this.onNewVillage, this)
        this.moveVillage.on(Node.EventType.TOUCH_END, this.onMoveVillage, this)
    }

    setData(zone: MapZone, data: Village) {
        this.zone = zone
        this.villageData = data
        let obj: any = MapZone.zoneTypeToNum(zone.typeId)
        
        this.woodNUm.string = obj.w
        this.mudNum.string = obj.m
        this.ironNum.string = obj.i
        this.foodNum.string = obj.f

        // 只有typeId = 28 (v_f6i4m4w4: 28) 才能迁移村庄
        if(zone.typeId != 28){
            this.moveVillage.active = false
            this.newVillage.position = v3(0, this.newVillage.position.y, 0)
        } 

    }
    async onNewVillage(){
        console.log(this.zone)
        console.log(this.villageData)
        let soldierList = Soldier.createBySoldiersString()
       
        if(soldierList[9].count < 3){
            UIManager.instance.modal('提示',`需要3个${soldierList[9].name}，才能建立新村庄`, false)
           
            return
        }
        
        await UIManager.instance.showLoading()
        let res = await VillageApi.createVillage(this.zone.zoneId)
        UIManager.instance.closeLoading()
        UIManager.instance.tip('军队已出发')
        console.log(res)
        UIManager.instance.closeUI(PrefabEnum.VillageInformation)
        WorldController.instance.refresh()
    }
    async onMoveVillage(){
        try {

            if(UserInfo.instance.isMainVillage){
                if(UserInfo.instance.villageData.villageObjList.length > 1){
                    await UIManager.instance.confrim({
                        title: '提示',
                        content: '有分村时，不能迁移主村'
                    })
                    return
                }                
            } 

            if(UserInfo.instance.diamond < 100){
                await UIManager.instance.confrim({
                    title: '提示',
                    content: '迁移村庄，将要花费100元宝,您的元宝不足！'
                })
                return
            }

            
            await UIManager.instance.confrim({
                title: '提示',
                content: '迁移村庄，将要花费100元宝'
            })
    
            let res = await MapApi.move({d:this.zone.zoneId})
            
            await WorldController.instance.refresh()
        } catch (error) {
            
        }
       

       
        UIManager.instance.clear()

    }
}


